#include "xmlsaxhandler.h"
#include <QMessageBox>


XMLSaxHandler::XMLSaxHandler(){
}

bool XMLSaxHandler::startDocument()
{
    inAdBook = false;
    inWorkspaceTag = false;
    inRobotTag = false;
    inPathTag = false;
    return true;
}

bool XMLSaxHandler::endElement( const QString&, const QString&, const QString &name )
{
    if( name == "path" )
        inPathTag = false;
    else if( name == "robot" ){
        inRobotTag = false;
    }
    else if( name == "workspace" )
        inWorkspaceTag = false;
    return true;
}

bool XMLSaxHandler::startElement( const QString&, const QString&, const QString &name, const QXmlAttributes &attrs )
{

    if( inPathTag && name == "cell" )
    {
        int x, y;
        for( int i=0; i<attrs.count(); i++ )
        {
            if( attrs.localName( i ) == "x" )
                x = attrs.value( i ).toInt();
            else if( attrs.localName( i ) == "y" )
                y = attrs.value( i ).toInt();
        }
        WorkspaceFactory::Instance().addCell(x,y);
    }
    else if( inRobotTag && name == "path" )
    {
        inPathTag = true;
    }
    else if( inWorkspaceTag && name == "robot" )
    {
        int id;
        inRobotTag = true;
        for( int i=0; i<attrs.count(); i++ )
        {
            if( attrs.localName( i ) == "id" )
                id = attrs.value( i ).toInt();
        }
        WorkspaceFactory::Instance().addRobot(id);
    }

    else if( name == "workspace" ){
        inWorkspaceTag = true;
        int size=0;
        for( int i=0; i<attrs.count(); i++ )
        {
            if( attrs.localName( i ) == "size" )
                size = attrs.value( i ).toInt();
        }
        WorkspaceFactory::Instance().createMatrix(size);
    }
    return true;
}
